package com.example.egweibo1.controller;

import com.example.egweibo1.base.response.JsonResult;
import com.example.egweibo1.base.response.Status;
import com.example.egweibo1.base.utils.CacheUtil;
import com.example.egweibo1.mapper.WeiboMapper;
import com.example.egweibo1.pojo.entity.Weibo;
import com.example.egweibo1.pojo.vo.UserVo;
import com.example.egweibo1.pojo.vo.WeiboDetailVO;
import com.example.egweibo1.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@Validated
@RequestMapping("/v1/weibo")
@Api(tags = "微博模块")
public class WeiboController {
    @Autowired(required = false)
    private WeiboMapper weiboMapper;
//    @Autowired
//    private RedisTemplate<String, Serializable> redisTemplate;
    @Autowired
    private CacheUtil cacheUtil;
    @PostMapping("/insert")
    @ApiOperation("发布微博")
    @ApiImplicitParam(name = "content",value = "微博内容",required = true)
    public JsonResult insertWeibo(@NotBlank @RequestParam String content, @ApiIgnore HttpSession session){
        UserVo userVo=(UserVo)session.getAttribute("user");
        if(userVo==null){
            return new JsonResult(Status.NO_LOGIN,"用户未登录");
        }
        Weibo weibo=new Weibo();
        weibo.setContent(content);
        weibo.setUserId(userVo.getId());
        weibo.setCreateTime(new Date());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.ok();
    }
    @ApiOperation("获取微博列表")
    @GetMapping("/selectIndex")
    public JsonResult getWeiboList(){
        log.info("------>开始获取微博列表");
        /*ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        Object weiboList = ops.get("weiboList");
        if (weiboList!=null){
            log.debug("从redis缓存中获取微博列表"+weiboList);
            return JsonResult.ok(weiboList);
        }*/
        Serializable weiboList = cacheUtil.get("weiboList");
        if (weiboList!=null){
            log.debug("从redis缓存中获取微博列表"+weiboList);
            return JsonResult.ok(weiboList);
        }
        log.debug("从mysql数据库中获取微博列表");
        List<WeiboIndexVO> weiboIndexVOS = weiboMapper.selectIndex();
//        ops.set("weiboList",(Serializable) weiboIndexVOS,30, TimeUnit.SECONDS);
        cacheUtil.setExpir("weiboList",(Serializable) weiboIndexVOS,30);
        return JsonResult.ok(weiboIndexVOS);
    }
    @GetMapping("/selectById/{id}")
    @ApiOperation("获取微博详情")
    @ApiImplicitParam(name = "id",value = "微博id",required = true,type="Long")
    public JsonResult getWeiboById(@PathVariable("id") Long id){
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }

}
